package com.dzj.fishingcommunity.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dzj.fishingcommunity.common.ResponseResult;
import com.dzj.fishingcommunity.model.PostComment;
import com.dzj.fishingcommunity.service.PostCommentService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/post-comments")
@Tag(name = "帖子评论管理", description = "帖子评论管理相关的接口")
public class PostCommentController {

    @Autowired
    private PostCommentService postCommentService;

    // 获取所有评论
    @GetMapping
    @Operation(summary = "获取所有评论")
    public ResponseResult<List<PostComment>> getAllPostComments(
            @Parameter(description = "当前页码", required = false) @RequestParam(defaultValue = "1") int current,
            @Parameter(description = "每页大小", required = false) @RequestParam(defaultValue = "10") int size) {
        Page<PostComment> page = new Page<>(current, size);
        Page<PostComment> postCommentPage = postCommentService.page(page);
        return ResponseResult.success(postCommentPage.getRecords());
    }

    // 根据ID获取评论
    @GetMapping("/{id}")
    @Operation(summary = "根据ID获取评论")
    public ResponseResult<PostComment> getPostCommentById(@Parameter(description = "评论ID", required = true) @PathVariable Integer id) {
        PostComment postComment = postCommentService.getById(id);
        if (postComment != null) {
            return ResponseResult.success(postComment);
        } else {
            return ResponseResult.error(404, "评论不存在");
        }
    }

    // 创建新评论
    @PostMapping
    @Operation(summary = "创建新评论")
    public ResponseResult<PostComment> createPostComment(@RequestBody PostComment postComment) {
        postCommentService.save(postComment);
        return ResponseResult.success(postComment);
    }

    // 更新评论


    // 删除评论
    @DeleteMapping("/{id}")
    @Operation(summary = "删除评论")
    public ResponseResult<Void> deletePostComment(@Parameter(description = "评论ID", required = true) @PathVariable Integer id) {
        boolean isDeleted = postCommentService.removeById(id);
        if (!isDeleted) {
            return ResponseResult.error(400, "评论删除出现异常");
        }
        return ResponseResult.success(null);
    }
}
